/**
 * 模拟模式对话框
 */
var yaModal=new Class({
    Implements:Options,
    options:{
        size:[400,300],/*大小是指主区域的大小，不包含标题栏*/
        opacity:0.5,
        zIndex:1000,
        modal:true,
        dragable:true,
        resizeable:false
    },
    isShow:false,
    draging:false,
    initialize:function(appendTo){
        var apt=$(appendTo);
        //遮罩层
        this.boxMask=new Element('div',{'class':'ymBase ymMask'});
        apt.grab(this.boxMask);
        //主层
        this.boxContainer=new Element('div',{'class':'ymBase ymMain'});
        apt.grab(this.boxContainer);
        //标题栏
        this.boxTitle=new Element('div',{'class':'ymTitle'});
        this.boxContainer.grab(this.boxTitle);
        //标题栏-关闭区域
        this.boxTitleClose=new Element('div',{'class':'ymTitle-close'});
        this.boxTitle.grab(this.boxTitleClose);
        //标题栏关闭按钮
        this.boxTitleCloseImg=new Element('div',{
            'class':'ymTitle-close-img',
            'title':Lang['yaModal_close'],
            'html':'<img src="'+_TMPL_+'pAdmin/img/close.gif'+'"/>'
        });
        this.boxTitleCloseImg.addEvents({
            'click':function(){this.close();}.bind(this),
            'mouseover':function(){ this.addClass('ymTitle-close-img-hover');},
            'mouseout':function(){ this.removeClass('ymTitle-close-img-hover');}
        });
        this.boxTitleClose.grab(this.boxTitleCloseImg);
        //标题栏--标题部分
        this.boxTitleLabel=new Element('div',{'class':'ymTitle-label'});
        this.boxTitle.grab(this.boxTitleLabel);
        //主内容
        this.boxMain=new Element('div');
        this.boxContainer.grab(this.boxMain);
        //拖拽效果
        if(this.options.dragable){
            new Drag.Move(this.boxContainer,{
                handle:this.boxTitleLabel,
                onBeforeStart:function(){ this.draging=true;}.bind(this)
            });
        }
        //大小改变效果
        if(this.options.resizeable){
            var resizeInst=this.boxContainer.makeResizable();
            resizeInst.addEvent('snap',function(){
                if(this.draging) resizeInst.stop();
                this.draging=false;
            }.bind(this));
        }
        //绑定大小调节函数
        $(window).addEvent('resize',function(){ this.resize(); }.bind(this));
    },
    loadOptions:function(opts){
        //类似于setOptions,但防止注入过多的内容
    },
    pack:function(fragment,autoSize){
        //autoSizeRefer 自适应大小时的参数物
        this.boxMain.empty();
        this.boxMain.grab(fragment);
        if(autoSize){
            this.boxContainer.setStyle('visibility','hidden');
            this.boxContainer.setStyle('display','block');
            var fsize = fragment.getSize();
            this.options.size[0] = fsize.x;
            this.options.size[1] = fsize.y;
        }

        var winpos=$(window).getSize();
        //对遮罩层进行设置
        if(this.options.modal){
            this.boxMask.setStyles({
                'width':winpos.x+'px',
                'height':winpos.y+'px',
                'opacity':this.options.opacity,
                'z-index':this.options.zIndex,
                'display':'block'
            });
        }else{ this.boxMask.setStyle('display','none');}
        //对标题进行设置
        this.boxTitleLabel.set('html',this.options.title || Lang['yaModal_default_title'] );
        //对主内容进行设置
        this.boxMain.setStyles({
            'height':(this.options.size[1])+'px',
            'width':(this.options.size[0])+'px'
        });
        //对主层进行设置
        var left=(winpos.x-this.options.size[0])/2;
        var top=(winpos.y-this.options.size[1])/3;
        this.boxContainer.setStyles({
            'left':(left>20)?left:20+'px',
            'top':(top>20)?top:20+'px',
            'z-index':this.options.zIndex+2,
            'width':(this.options.size[0])+'px',
            'visibility':'visible',
            'display':'block'
        });
        this.isShow=true;
    },
    resize:function(){
        if(this.isShow){
            var winpos=$(window).getCoordinates();
            this.boxMask.setStyles({
                'width':winpos.width+'px',
                'height':winpos.height+'px'
            });
            //对主层进行设置
            var left=(winpos.width-this.options.size[0])/2;
            var top=(winpos.height-this.options.size[1])/3;
            this.boxContainer.setStyles({
                'left':(left>20)?left:20+'px',
                'top':(top>20)?top:20+'px'
            });
        }
    },
    close:function(){
        this.boxMask.setStyle('display','none');
        this.boxContainer.setStyle('display','none');
    },
    html:function(opts){
        this.loadOptions(opts);
        var iframe=new Element('iframe',{
            'class':'modalIframe',
            'frameborder':'0',
            'scrolling':'no',
            'src':opts.src,
            'width':'100%',
            'height':'100%'
        });
        this.pack(iframe);
    },
    prompt:function(opts){
        //opts:message,title,defaultValue,onReturn
        this.loadOptions(opts);
        //弹出一个输入框
        var div = new Element('div',{
            'class':'ym-prompt clearfix',
            'html': (opts.message ? '<h5>'+opts.message+'</h5>' : '')+
                   '<div><input type="text" class="textInput"/></div>'+
                   '<div class="ymButton"><button class="btn" type="button"/>'+Lang['yaModal_button_ok']+'</button><button class="btn" type="button">'+Lang['yaModal_button_cancel']+'</button></div>',
            'events':{
                'click':function(e){
                    var target = $(e.target);
                    if(target.get('tag')=='button'){
                        if(target.get('html') == Lang['yaModal_button_ok'] ){
                            var ips = target.getParent().getPrevious().getFirst();
                            returnVal = ips.getProperty('value');
                            if(opts.onReturn && false===opts.onReturn(returnVal,ips)){ //onReturn 返回false时不继续执行
                                ips.select();
                                return;
                            }
                        }
                        this.close();
                    }
                }.bind(this)
            }
        });
        this.options.title = opts.title || Lang['yaModal_prompt_defTitle'];
        this.pack(div,true);
        div.getElement('input').focus();
    }
});
